﻿@inject IDataConfigService DataConfigService

<MudDialog>
    <DialogContent>
        <MudForm @ref="_editRequestForm" Model="_editRequestDto" Validation="@(_editValidator.ValidateValue)" ValidationDelay="0">
            <MudGrid>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.ConfigCode" For="@(() => _editRequestDto.ConfigCode)" Label="配置编码"></MudTextField>
                </MudItem>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.ConfigName" For="@(() => _editRequestDto.ConfigName)" Label="配置名称"></MudTextField>
                </MudItem>
                <MudItem xs="6">
                    <MudSelect @bind-Value="_editRequestDto.CheckWay" For="@(() => _editRequestDto.CheckWay)" Label="默认校验方式" Dense="true" AnchorOrigin="Origin.BottomCenter">
                        <MudSelectItem Value="@("InMemory")">InMemory</MudSelectItem>
                        <MudSelectItem Value="@("Database")">数据库</MudSelectItem>
                    </MudSelect>
                </MudItem>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.DataTable" For="@(() => _editRequestDto.DataTable)" Label="存储表名"></MudTextField>
                </MudItem>
                <MudItem xs="6">
                    <MudNumericField @bind-Value="_editRequestDto.Sort" For="@(() => _editRequestDto.Sort)" Label="排序"></MudNumericField>
                </MudItem>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.Description" For="@(() => _editRequestDto.Description)" Label="描述"></MudTextField>
                </MudItem>
            </MudGrid>
        </MudForm>
    </DialogContent>
    <DialogActions>
        <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="SubmitHandle">保存</MudButton>
        <MudButton Variant="Variant.Filled" OnClick="CancelHandle">取消</MudButton>
    </DialogActions>
</MudDialog>


@code {


    private DataConfigEditRequestDto _editRequestDto = new();

    private DataConfigEditValidator _editValidator = new();

    private MudForm _editRequestForm = new();

    [CascadingParameter]
    public MudDialogInstance? MudDialog { get; set; }

    [Parameter]
    public Guid? Key { get; set; }

    void CancelHandle()
    {
        if (MudDialog != null)
        {
            MudDialog.Cancel();
        }
    }

    async Task SubmitHandle()
    {
        await _editRequestForm.Validate();

        if (Key != null)
        {
            var result = await DataConfigService.UpdateDataConfig(Key.Value, _editRequestDto);

            if (result.Succeeded && MudDialog != null)
            {
                MudDialog.Close(DialogResult.Ok(true));
            }
        }
        else
        {
            var result = await DataConfigService.CreateDataConfig(_editRequestDto);

            if (result.Succeeded && MudDialog != null)
            {
                MudDialog.Close(DialogResult.Ok(true));
            }
        }
    }

    protected override async Task OnParametersSetAsync()
    {
        _editRequestDto = new DataConfigEditRequestDto();
        if (Key != null)
        {
            var result = await DataConfigService.GetDataConfig(Key.Value);
            if (result.Succeeded && result.Data != null)
            {
                _editRequestDto.ConfigCode = result.Data.ConfigCode;
                _editRequestDto.ConfigName = result.Data.ConfigName;
                _editRequestDto.CheckWay = result.Data.CheckWay;
                _editRequestDto.DataTable = result.Data.DataTable;
                _editRequestDto.Sort = result.Data.Sort;
                _editRequestDto.Description = result.Data.Description;
            }
        }
    }

}